import { QrcodeconfigService } from './../../../../@core/services/QRCodeConfig/qrcodeconfig.service';
import { Component } from '@angular/core';
import { ReviewRolesService } from 'src/app/@core/services/ReviewRoles/review-roles.service';
import { DialogService, TableWidthConfig } from 'ng-devui';
import { QRCodeConfigAddComponent } from '../qrcode-config-add/qrcode-config-add.component';
import { QRCodeConfig } from 'src/app/@core/data/QRCodeConfig';
@Component({
  selector: 'app-purchase-qrcodeconfig',
  templateUrl: './purchase-qrcodeconfig.component.html',
  styleUrls: ['./purchase-qrcodeconfig.component.scss'],
})
export class PurchaseQrcodeconfigComponent {
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20,
  };
  pageSizeOptions = [10, 20, 30, 50, 100];
  msgs: { severity: any; summary: any; content: any }[];
  QRCodeConfigs: QRCodeConfig[] = [
  ]; //审批角色
  dataTableOptions = {
    columns: [
      {
        field: 'name',
        header: '名称',
        fieldType: 'text',
      },
      {
        field: 'type',
        header: '描述',
        fieldType: 'text',
      },
      {
        field: 'type',
        header: '是否默认',
        fieldType: 'text',
      },
    ],
  }; //表格数据
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: '操作',
      width: '150px',
    },
    {
      field: 'name',
      width: '',
    },
    {
      field: 'type',
      width: '',
    },
    {
      field: 'type',
      width: '',
    },
  ];
  config = {
    id: 'Add-Dirctoy-Type',
    maxHeight: '400px',
    backdropCloseable: true,
  }; //弹窗配置
  str = 'hello';
  constructor(
    private ReviewRolesService: ReviewRolesService,
    private dialogService: DialogService,
    private QrcodeconfigService: QrcodeconfigService
  ) {}
  ngOnInit(): void {
    this.QrcodeconfigService.getQRCodeConfigurations(
      '采购到货二维码配置',
      this.pager.pageIndex,
      this.pager.pageSize
    ).subscribe((data:any) => {
      this.QRCodeConfigs = data.items;
    });
  }
  pageIndexChange($event: any) {} //切换页码
  pageSizeChange($event: any) {} //改变每页显示数量
  delete(id: any, dialogtype?: string) {
    const results = this.dialogService.open({
      ...this.config,
      width: '400px',
      maxHeight: '600px',
      title: '删除该配置',
      content: '确认删除该配置？',
      dialogtype: dialogtype,
      onClose: () => {},
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            this.QrcodeconfigService.deleteQRCodeConfiguration([id]).subscribe((res:any)=>{
                this.showToast('success','成功','删除成功')
                this.QrcodeconfigService.getQRCodeConfigurations(
                  '采购到货二维码配置',
                  this.pager.pageIndex,
                  this.pager.pageSize
                ).subscribe((data:any) => {
                  this.QRCodeConfigs = data.items;
                });
                results.modalInstance.hide();

            },(error:any)=>{
              this.showToast('error','错误',error.error || '删除失败')
            }
          )
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  } //删除
  showToast(type: any, summary: any, content: any) {
    switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  } //提示框
  addQRCode() {
    let data = {
      warning: false,
      type:'采购到货二维码配置',
    };
    const results = this.dialogService.open({
      ...this.config,
      width: '800px',
      maxHeight: '600px',
      title: '新增二维码配置',
      data: data,
      content: QRCodeConfigAddComponent,
      onClose: () => {},
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler: ($event: Event) => {
            if(results.modalContentInstance.QRCodeConfig.qrCodeConfigurationName){
              this.QrcodeconfigService.createQRCodeConfiguration(results.modalContentInstance.QRCodeConfig).subscribe((res:any)=>{
                  this.showToast('success','成功','新增成功')
                  this.QrcodeconfigService.getQRCodeConfigurations(
                    '采购到货二维码配置',
                    this.pager.pageIndex,
                    this.pager.pageSize
                  ).subscribe((data:any) => {
                    this.QRCodeConfigs = data.items;
                  });
                  results.modalInstance.hide();

              },(error:any)=>{
                this.showToast('error','错误',error.error || '新增失败')
                data.warning=true;
              }
            )
            }else{
              this.showToast('error','错误','名称不能为空')
              data.warning=true;
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  } //编辑
  editQRCode(rowItem: any) {
    let data = {
      warning: false,
      rowItem: rowItem,
      type:'采购到货二维码配置',
    };
    const results = this.dialogService.open({
      ...this.config,
      width: '800px',
      maxHeight: '600px',
      title: '更新二维码配置',
      data: data,
      content: QRCodeConfigAddComponent,
      onClose: () => {},
      buttons: [
        {
          cssClass: 'stress',
          text: '确定',
          handler:($event: Event) => {
            if(results.modalContentInstance.QRCodeConfig.qrCodeConfigurationName){
              this.QrcodeconfigService.updateQRCodeConfiguration(results.modalContentInstance.QRCodeConfig,rowItem.qrCodeConfigurationId).subscribe((res:any)=>{
                  this.showToast('success','成功','更新成功')
                  this.QrcodeconfigService.getQRCodeConfigurations(
                    '采购到货二维码配置',
                    this.pager.pageIndex,
                    this.pager.pageSize
                  ).subscribe((data:any) => {
                    this.QRCodeConfigs = data.items;
                  });
                  results.modalInstance.hide();

              },(error:any)=>{
                this.showToast('error','错误',error.error || '更新失败')
                data.warning=true;
              }
            )
            }else{
              this.showToast('error','错误','名称不能为空')
              data.warning=true;
            }
          },
        },
        {
          id: 'btn-cancel',
          cssClass: 'common',
          text: '取消',
          handler: ($event: Event) => {
            results.modalInstance.hide();
          },
        },
      ],
    });
  } //编辑
}
